package com.agh.portal.domain;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

import com.agh.portal.data.Votable;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findCommentsByStudent", "findCommentsByMainPagePost" })
public class Comment implements Votable {

	@NotEmpty
    @Size(max = 1000)
    private String content;

    private Integer goodVoteCount = 0;

    private Integer badVoteCount = 0;
    
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Set<Student> proVoters = new HashSet<Student>();

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Set<Student> conVoters = new HashSet<Student>();

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date creationDate;

    @NotNull
    @ManyToOne
    private MainPagePost mainPagePost;

    @NotNull
    @ManyToOne
    private Student student;

    public String toString() {
        return "Comment [ content=" + content + " goodVoteCount=" + goodVoteCount + " badVoteCount=" + badVoteCount + " creationDate=" + creationDate + " mainPagePost=" + mainPagePost.getTitle() + " student=" + student.getName() + "]";
    }

    public static List<Comment> findAllComments() {
        return entityManager().createQuery("SELECT o FROM Comment o ORDER BY o.creationDate desc", Comment.class).getResultList();
    }

    public static List<Comment> findCommentEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Comment o ORDER BY o.creationDate desc", Comment.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    public static TypedQuery<Comment> findLatestCommentsByStudent(Student student, Integer maxResults) {
        if (student == null) throw new IllegalArgumentException("The student argument is required");
        EntityManager em = Comment.entityManager();
        TypedQuery<Comment> q = em.createQuery("SELECT o FROM Comment AS o WHERE o.student = :student ORDER BY o.creationDate desc", Comment.class);
        q.setParameter("student", student);
        if (maxResults != null) {
            q.setMaxResults(maxResults);
        }
        return q;
    }
    
    public static TypedQuery<Comment> findLatestComments(Integer maxResults) {
        EntityManager em = Comment.entityManager();
        TypedQuery<Comment> q = em.createQuery("SELECT o FROM Comment AS o ORDER BY o.creationDate desc", Comment.class);
        if(maxResults != null){
        	q.setMaxResults(maxResults);
        }
        return q;
    }

    public void addGoodVote() {
        this.goodVoteCount++;
    }

    public void decrementGoodVote() {
        this.goodVoteCount--;
    }

    public void addBadVote() {
        this.badVoteCount++;
    }

    public void decrementBadVote() {
        this.badVoteCount--;
    }

}
